<template>
  <div ref="echartDom"></div>
</template>

<script setup>
import {ref, defineProps, onMounted, onUnmounted, inject, shallowRef, defineExpose} from 'vue'
import {pxToRem} from "@/utils/index";

const props = defineProps({
  barData: {
    type: Object,
    default: () => {}
  },
  dialogData: {
    type: Object,
    default: () => ({
      fontSizeTimes: 1
    })
  }
});

const echartDom = ref(null);
const myChart = shallowRef(null);
const echarts = inject('echarts');

const initEchart = () => {
  if (echartDom.value) {
    myChart.value = echarts.init(echartDom.value);
    let tempSeries = []
    tempSeries.push({
      type: 'bar',
      data: props?.barData?.seriesData,
    })
    let option = {
      ...props?.barData?.colorConfig,
      tooltip: {
        trigger: 'axis',
        valueFormatter: (val) => `${val}${props?.barData?.tooltipUnit || props?.barData?.yAxisName}`
      },
      barMaxWidth: pxToRem(11 * props?.dialogData?.fontSizeTimes),
      grid: {
        left: '3%',
        right: '2%',
        bottom: 0,
        top: `${props?.dialogData?.fontSizeTimes === 1 ? 27 : 12}%`,
        containLabel: true
      },
      dataZoom: { // 数据过多拖动逻辑
        type: "slider",
        startValue: 0,
        height: pxToRem(20),
        bottom: 0
      },
      xAxis: {
        type: 'category',
        axisTick: {
          show: false // 不显示坐标轴刻度线
        },
        axisLine: {
          show: true, // 不显示坐标轴线
        },
        splitLine: {
          show: false // 不显示网格线
        },
        axisLabel: {
          color: 'white',
          fontSize: pxToRem(9 * props?.dialogData?.fontSizeTimes),
          interval: props?.dialogData?.fontSizeTimes === 1 ? 'auto' : 0,
          ...props?.barData?.axisLabelConfig
        },
        data: props?.barData?.xAxisData
      },
      yAxis: {
        name: props?.barData?.yAxisName,
        min: 0,
        nameTextStyle: {
          color: 'white',
          fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes),
          padding: [0, 0, 0, pxToRem(-25 * props?.dialogData?.fontSizeTimes)]
        },
        nameGap: pxToRem(props?.dialogData?.fontSizeTimes === 1 ? 15 : 25),
        axisTick: {
          show: false // 不显示坐标轴刻度线
        },
        axisLine: {
          show: true, // 不显示坐标轴线
        },
        splitLine: {
          show: false // 不显示网格线
        },
        axisLabel: {
          fontSize: pxToRem(12 * props?.dialogData?.fontSizeTimes),
          color: 'white'
        }
      },
      series: tempSeries
    }
    option && myChart.value.setOption(option, true);
  }
}

const updateChart = () => {
  if (myChart.value) {
    myChart.value.dispose(); // 销毁旧图表实例
  }
  initEchart();
};

defineExpose({
  updateChart
})

onMounted(() => {
  initEchart();
  window.addEventListener("resize", updateChart);
})

onUnmounted(() => {
  window.removeEventListener("resize", updateChart);
})
</script>

<style scoped lang="less">
.h135 {
  height: 135px;
}
.dialog-h135 {
  width: 1120px;
  height: 500px;
}
.h150 {
  height: 150px;
}
.dialog-h150 {
  width: 1120px;
  height: 500px;
}
.h170 {
  height: 170px;
}
.dialog-h170 {
  width: 1120px;
  height: 500px;
}
</style>